from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select


def open_browser(browser="谷歌"):
    '''
    打开浏览器,最大化窗口
    :param browser: 默认打开谷歌浏览器
    :return: 浏览器对象
    '''
    if browser == "谷歌":
        driver = webdriver.Chrome()
    elif browser == "火狐":
        driver = webdriver.Firefox()
    elif browser == "ie":
        driver = webdriver.Ie()
    else:
        raise Exception(f"{browser}浏览器打开失败,支持的浏览器有: 谷歌,火狐,ie")
    driver.maximize_window()
    return driver


def open_op_browser(path):
    """
    使用火狐加载项打开浏览器
    :return:
    """
    user_data = path
    # 实例化加载项配置文件对象
    option = webdriver.FirefoxProfile(user_data)
    # 打开浏览器使用 配置文件打开
    driver = webdriver.Firefox(option)
    # driver.maximize_window()
    return driver


class Base(object):
    def __init__(self, driver):
        '''
        初始化属性
        :param driver: 浏览器对象
        '''
        self.driver = driver

    def open_url(self, url):
        '''
        打开网页
        :param url: 网页地址
        :return: 
        '''
        self.driver.get(url)

    def find_element(self, locator, timeout=10):
        '''
        定位元素
        :param locator: 定位方式和值[字典]
        :param timeout: 等待时长
        :return: 元素对象
        '''
        return WebDriverWait(self.driver, timeout=timeout).until(ec.presence_of_element_located(locator))

    def find_elements(self, locator, timeout=10):
        """
        定位多个元素
        :param locator: 定位方式和值[字典]
        :param timeout: 等待时长
        :return: 元素对象是一个列表
        """
        return WebDriverWait(self.driver, timeout=timeout).until(ec.presence_of_all_elements_located(locator))

    def send_text(self, locator, text, timeout=10):
        '''
        定位元素并输入内容
        :param locator: 定位方式和值[字典]
        :param text: 输入的内容
        :param timeout: 等待时长
        :return: 
        '''
        self.find_element(locator, timeout=timeout).send_keys(text)

    def click(self, locator, timeout=10):
        '''
        定位并点击元素
        :param locator: 定位方式和值[字典]
        :param timeout: 等待时长
        :return: 
        '''
        self.find_element(locator, timeout=timeout).click()

    def get_element_text(self, locator, timeout=10):
        '''
        获取元素文本
        :param locator: 定位方式和值[字典]
        :param timeout: 等待时长
        :return: 元素文本
        '''
        return self.find_element(locator, timeout=timeout).text

    def switch_to_frame(self, frame):
        '''
        切换表单
        :param frame:表单对象|表单id属性值|表单name属性值
        :return: None
        '''
        if isinstance(frame, str):
            self.driver.switch_to.frame(frame)
        else:
            self.driver.switch_to.frame(self.find_element(frame))


    def switch_to_parent_frame(self):
        """
        返回上级表单
        :return:
        """
        self.driver.switch_to.parent_frame()

    def select_by_text(self, select_locator, selector):
        '''
        下拉框选择
        :param select_locator: 下拉框定位方式,是个元祖
        :param selector: 选择依据[选项索引(0开始)或者选项文本]
        :return:
        '''
        self.slct = Select(self.find_element(select_locator))
        if isinstance(selector, str):
            self.slct.select_by_visible_text(selector)
        elif not selector:
            pass
        else:
            self.slct.select_by_index(selector)

    def close(self):
        '''
        关闭网页
        :return:
        '''
        self.driver.close()

    def quit(self):
        '''
        退出浏览器
        :return: 
        '''
        self.driver.quit()

    def title(self):
        """
        获取页面标题
        :return:
        """
        return self.driver.title

    def clear_send_text(self, locator, text, timeout=10):
        """
        清空输入框内的信息重新输入
        :return:
        """
        self.find_element(locator, timeout=timeout).clear()
        self.find_element(locator, timeout=timeout).send_keys(text)

    def is_text_in_element(self, locator, text, timeout=10):
        """
        判断文本是否存在于元素中,如果存在返回True,不存在返回False
        :param locator: 定位器
        :param text: 验证的文本
        :return:
        """
        try:
            result = WebDriverWait(self.driver, timeout=timeout).until(ec.text_to_be_present_in_element(locator, text))
            return result
        except:
            print("元素未找到或元素不存在")
            return False

    def get_value(self, locator, timeout=10):
        '''
        获取网页元素的value
        :return:
        '''
        return self.find_element(locator, timeout=timeout).get_attribute("value")
